package com.liangsui.mss.controller.system;

import com.liangsui.mss.aspectj.Log;
import com.liangsui.mss.aspectj.enums.BusinessType;
import com.liangsui.mss.dto.DeptDto;
import com.liangsui.mss.service.DeptService;
import com.liangsui.mss.utils.ShiroSecurityUtils;
import com.liangsui.mss.vo.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * DeptController
 *
 * @author zhangjiawei
 * @email 1533167396@qq.com
 * @date 2022/12/5 15:54
 */
@RestController
@RequestMapping("system/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     * @功能描述: 科室分页查询
     */
    @GetMapping("listForPage")
    public AjaxResult listForPage(DeptDto deptDto) {
        return AjaxResult.success(deptService.listPage(deptDto));
    }

    /**
     * @功能描述:查询所有可用的科室
     */
    @GetMapping("selectAllDept")
    public AjaxResult selectAllDept() {
        return AjaxResult.success(this.deptService.listDept());
    }

    /**
     * @功能描述: 科室添加
     */
    @PostMapping("addDept")
    @Log(title = "科室添加", businessType = BusinessType.INSERT)
    public AjaxResult addDept(@Validated DeptDto deptDto) {
        // 设置添加人员
        deptDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.deptService.addDept(deptDto));
    }

    /**
     * @功能描述: 科室修改
     */
    @PutMapping("updateDept")
    @Log(title = "科室修改", businessType = BusinessType.UPDATE)
    public AjaxResult updateDept(@Validated DeptDto deptDto) {
        try {
            // 设置修改人员
            deptDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
            return AjaxResult.toAjax(this.deptService.updateDept(deptDto));
        } catch (Exception e) {
            return AjaxResult.ex(e.getMessage());
        }
    }

    /**
     * @功能描述: 根据ID查询一个科室信息
     */
    @GetMapping("getDeptByID/{dictId}")
    @Log(title = "根据ID查询一个科室信息", businessType = BusinessType.SELECT)
    public AjaxResult getDeptByID(@NotNull(message = "科室ID不能为空")
                                      @PathVariable @Validated Long dictId) {
        try {
            return AjaxResult.success(this.deptService.getDeptById(dictId));
        }catch (Exception e){
            return AjaxResult.ex(e.getMessage());
        }
    }

    /**
     * @功能描述:删除
     */
    @DeleteMapping("deleteDeptByIds/{dictIds}")
    @Log(title = "根据ID删除科室", businessType = BusinessType.DELETE)
    public AjaxResult updateDept(@PathVariable @Validated
                                     @NotEmpty(message = "要删除的科室ID不能为空") Long[] dictIds) {
        try {
            return AjaxResult.toAjax(this.deptService.deleteDeptByIds(dictIds));
        }catch (Exception e){
            return AjaxResult.ex(e.getMessage());
        }
    }
}
